<template>
    <view>
        <view v-if="detail != null" class="bigpage">
            <!--宣传语-->
            <view class="kan_box">
                <view class="kan_body">
                    <view class="user-info">
                        <image class="img1" :src="detail.user.avatar" mode="aspectFit" />
                        <view class="user-name">{{ detail.user.user_name_view }}</view>
                    </view>
                    <block v-if="(detail.cut_info || null) != null">
                        <view class="p1" v-if="detail.cut_info.share_ftitle != ''">
                            <text class="tt11">“</text>
                            {{ detail.cut_info.share_ftitle }}
                            <text class="tt22">”</text>
                        </view>
                        <view class="p1" v-else>
                            <text class="tt11">“</text>
                            {{ plugins_config.share_ftitle }}
                            <text class="tt22">”</text>
                        </view>
                    </block>

                    <!-- 商品列表 -->
                    <view class="goods bg-white">
                        <block v-if="(detail.cut_info || null) != null">
                            <view class="goods-item oh">
                                <navigator :url="'/pages/fcc/cutprice/detail/detail?cut_id=' + detail.goods_id" hover-class="none">
                                    <image class="goods-image fl" :src="detail.images" mode="aspectFill" />
                                    <view class="goods-base">
                                        <view class="goods-title multi-text">{{ detail.title }}</view>
                                        <block v-if="detail.spec != null">
                                            <view class="goods-attribute cr-888" v-for="(spec, index) in detail.spec" :key="index">{{ spec.type }}:{{ spec.value }}</view>
                                        </block>
                                    </view>
                                </navigator>
                            </view>
                        </block>
                        <block v-else>
                            <view class="goods-item oh tc">{{$t('fengcheche.cutprice.cart_detail_already_done_msg')}}</view>
                        </block>
                        <view class="order-describe">
                            <block v-if="(detail.cut_info || null) != null">
                                <view class="jindutext">
                                    <text class="fr">{{$t('fengcheche.cutprice.cart_detail_price_desc_before')}}{{ currency_symbol}}{{ detail.low_price}}{{$t('fengcheche.cutprice.cart_detail_price_desc_after')}}</text>
                                    <text v-if="detail.original_price > 0" class="">{{$t('fengcheche.cutprice.list_price_old_price')}}{{ currency_symbol }}{{ detail.original_price }}</text>
                                </view>
                                <view class="jindutiao">
                                    <progress :percent="detail.now_jindu" stroke-width="10" border-radius="8" activeColor="#fec330" active />
                                </view>
                                <view class="jindudesc tc">
                                    {{$t('fengcheche.cutprice.cart_detail_cut_desc_before')}}
                                    <text class="t1">{{ currency_symbol }}{{ detail.already_kan }}</text>{{$t('fengcheche.cutprice.cart_detail_cut_desc_after')}}
                                    ，{{$t('fengcheche.cutprice.cart_detail_need_desc_before')}}
                                    <text class="t1">{{ currency_symbol }}{{ detail.now_need_kan }}</text>{{$t('fengcheche.cutprice.cart_detail_need_desc_after')}}
                                    <!-- 时间 -->
                                    <text v-if="detail.status == 0 && detail.time.status == 1" class="timebox">
                                        <text>，{{$t('fengcheche.cutprice.cart_detail_over_desc_before')}}</text>
                                        <text class="shi t1">{{ detail.time.hours }}</text>
                                        <text class="dian t1">{{$t('fengcheche.cutprice.cart_detail_time_text_1')}}</text>
                                        <text class="fen t1">{{ detail.time.minutes }}</text>
                                        <text class="dian t1">{{$t('fengcheche.cutprice.cart_detail_time_text_2')}}</text>
                                        <text class="miao t1">{{ detail.time.seconds }}</text>
                                        <text class="dian t1">{{$t('fengcheche.cutprice.cart_detail_time_text_3')}}</text>
                                        <text>{{$t('fengcheche.cutprice.cart_detail_over_desc_after')}}</text>
                                    </text>
                                </view>
                            </block>
                            <view :class="'jindubtn '+$t('fengcheche.cutprice.app_lang')">
                                <block v-if="detail.status > 0">
                                    <view class="jindubtn_cc jd01"><view data-value="/pages/index/index" data-type="1" @tap="navigation_event">{{$t('fengcheche.cutprice.app_shop_index_page_btn_text')}}</view></view>
                                    <view class="jindubtn_cc jd02"><view>{{$t('fengcheche.cutprice.cart_detail_fail_text')}}</view></view>
									<view class="cl"></view>
                                </block>
                                <block v-else>
                                    <!--未失效的砍价，分两种情况，一种还在砍，另外一种已经砍到最底价，正在砍也分两种，能够不到底价购买和必须到底价才能买-->
                                    <block v-if="detail.price == detail.low_price">
                                        <view class="jindubtn_cc jd01 jindu_big"><view :data-id="detail.id" @tap="buy_submit_event">{{$t('fengcheche.cutprice.cart_detail_go_buy_text')}}</view></view>
                                    </block>
                                    <block v-else>
                                        <block v-if="detail.cut_info.buy_type == 0">
                                            <view class="jindubtn_cc jd01"><view :data-id="detail.id" @tap="buy_submit_event">{{$t('fengcheche.cutprice.cart_detail_go_buy_text_2')}}</view></view>
                                            <view class="jindubtn_cc jd02">
                                                <view>
													<!-- #ifdef H5 -->
													<button class="dis-block" type="default" size="default" @tap="share_h5_event" hover-class="none">
													    <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
													</button>
													<!-- #endif -->
													<!-- #ifdef MP-ALIPAY -->
													<button class="dis-block" type="default" size="default" @tap="share_base_event" hover-class="none">
													    <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
													</button>
													<!-- #endif -->
													<!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO || MP-KUAISHOU -->
													<button class="dis-block" type="default" size="default" open-type="share" hover-class="none">
													    <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
													</button>
													<!-- #endif -->
													<!-- #ifdef APP-PLUS -->
													<button class="dis-block" size="default" hover-class="none" @tap="popup_share_event">
                                                        <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
                                                    </button>
													<!-- #endif -->
                                                </view>
                                            </view>
											<view class="cl"></view>
                                        </block>
                                        <block v-else>
											<view class="jindubtn_cc jd01"><view data-value="/pages/index/index" data-type="1" @tap="navigation_event">{{$t('fengcheche.cutprice.app_shop_index_page_btn_text')}}</view></view>
                                            <view class="jindubtn_cc jd02"><!--jindu_big-->
                                                <view>
													<!-- #ifdef H5 -->
													<button class="dis-block" type="default" size="default" @tap="share_h5_event" hover-class="none">
													    <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
													</button>
													<!-- #endif -->
													<!-- #ifdef MP-ALIPAY -->
													<button class="dis-block" type="default" size="default" @tap="share_base_event" hover-class="none">
													    <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
													</button>
													<!-- #endif -->
													<!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO || MP-KUAISHOU -->
													<button class="dis-block" type="default" size="default" open-type="share" hover-class="none">
													    <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
													</button>
													<!-- #endif -->
													<!-- #ifdef APP-PLUS -->
													<button class="dis-block" size="default" hover-class="none" @tap="popup_share_event">
													    <text>{{$t('fengcheche.cutprice.cart_detail_call_friend_text')}}</text>
													</button>
													<!-- #endif -->
                                                </view>
                                            </view>
											<view class="cl"></view>
											<view class="gohome"><view data-value="/pages/index2/index2" data-type="1" @tap="navigation_event">>> {{$t('fengcheche.cutprice.app_shop_index_page_btn_text')}} <<</view></view>
                                        </block>
                                    </block>
                                </block>
                            </view>
                        </view>
                    </view>
                </view>
            </view>

            <view class="spacing-nav-title">
                <text class="line"></text>
                <text class="text-wrapper">{{$t('fengcheche.cutprice.detail_base_title_guize_text')}}</text>
            </view>
            <view class="wanfa spacing">
                <view class="wanfacc weui-flex bg-white">
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>1</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_1')}}
                        </view>
                    </view>
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>2</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_2')}}
                        </view>
                    </view>
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>3</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_3')}}
                        </view>
                    </view>
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>4</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_4')}}
                        </view>
                    </view>
                </view>
            </view>

            <view class="spacing-nav-title">
                <text class="line"></text>
                <text class="text-wrapper">{{$t('fengcheche.cutprice.cart_detail_who_help_me_title')}}</text>
            </view>
            <view class="panel-item spacing-mt teamlist">
                <view class="panel-content bg-white">
                    <block v-if="(detail.teamusers || null) != null && detail.teamusers.length > 0">
                        <view class="tuan_li" v-for="(item, index) in detail.teamusers" :key="index">
                            <image :src="item.user.avatar" mode="aspectFill" />

                            <view class="whois">
                                <text class="t1">{{ item.user.user_name_view }}</text>
                                <text class="t2" :decode="true">{{ item.add_time }}</text>
                            </view>

                            <view class="liright">
                                <view class="tinfo">{{$t('fengcheche.cutprice.cart_detail_cut_howmuch_before')}}{{ currency_symbol }}{{ item.price }}{{$t('fengcheche.cutprice.cart_detail_cut_howmuch_after')}}</view>
                            </view>
                        </view>
                    </block>
                    <block v-else>
                        <view v-if="detail.teamusers.length == 0" class="tuan_li tc none_jilu">{{$t('fengcheche.cutprice.cutprice_no_record')}}</view>
                    </block>
                </view>
            </view>
			
			<!-- 分享弹窗 -->
			<component-share-popup :diyTheme="diytheme" ref="share"></component-share-popup>
        </view>

        <view v-if="detail == null">
            <!--<view class="kan_box">
                <view class="kan_body">
                    <view class="goods bg-white">
                        <view class="goods-item oh tc">该砍价已失效</view>
                    </view>
                </view>
            </view>-->
            <!-- 提示信息 -->
            <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
        </view>
    </view>
</template>

<script>
const app = getApp();
import componentNoData from "@/components/no-data/no-data";
import { cutprice_base_info } from '@/pages/fcc/cutprice/public/common.js';
import componentSharePopup from "@/components/share-popup/share-popup";

export default {
	components: {
		componentNoData,
		componentSharePopup
	},
    data() {
        return {
            currency_symbol: app.globalData.currency_symbol(),
            params: null,
            data_list_loding_status: 1,
            data_list_loding_msg: '',
            data_bottom_line_status: false,
            detail: null,
            site_fictitious: null,
            plugins_config: null,
            avatar: '',
            user_name_view: '',
            share_ftitle: '',
            status: 0,
            hours: '',
            minutes: '',
            seconds: '',
            buy_type: 0,
            length: 0,
			// 自定义分享信息
			share_info: {},
			// 多语言
			lang_info:{},
			diytheme:null
        };
    },
    onLoad(params) {
        //params['id'] = 13;
        this.setData({
            params: params
        });
		this.init();
    },
	onShow() {
		// 每次到了自己的砍价情况页面，都要先清理剪贴板及缓存，避免返回后再次回跳
		app.globalData.clear_linker_cut_cache();
		
		// #ifdef APP-PLUS
	    plus.key.addEventListener('backbutton', function() {
	      // 可以在这里执行你想要的操作
	      // 例如，关闭当前页面或者提示用户是否退出应用
	      //uni.navigateBack(2); // 返回上一页面
		  // 判断是否是自己返回，是自己返回，则返回到首页
		  const value = uni.getStorageSync('linker_cut_is_self_url_flag_cache_key') || null;
		  if((value || null) != null){
			  // 清理后返回
			  uni.removeStorageSync('linker_cut_is_self_url_flag_cache_key');
			  setTimeout(function () {
			  	uni.redirectTo({
			  		url: '/pages/index/index'
			  	});
			  }, 1000);
		  }
	    }, false);
		// #endif
	},
	onHide() {
		// #ifdef APP-PLUS
	    plus.key.removeEventListener('backbutton');
		// #endif
	},
    // 下拉刷新
    onPullDownRefresh() {
        this.init();
    },
	// 自定义分享
	onShareAppMessage() {
	    return {
			title: (this.detail.cut_info.share_title != '') ? this.detail.cut_info.share_title : data.plugins_config.share_title,
			kds: this.detail.title,
			desc: this.detail.title,
			img: this.detail.images,
	        path: '/pages/fcc/cutprice/cart/share/share?id=' + this.detail.id
	    };
	},
    methods: {
		// 加载之前
		init() {
			// 先看是否登录
			var user = app.globalData.get_user_cache_info();
			if(user == null) {
				var str = app.globalData.get_page_url();
				str = str.replace('detail/detail','share/share');
				
				uni.redirectTo({
					url: str
				});
				return;
			}
			cutprice_base_info()
			.then(data=>{
				this.init_before(data)
				.then(()=>{
					this.init_real();
				});
			});
		},
		// 设置本页参数
		init_before(data){
			var self = this;
			return new Promise((resolve, reject) => {
				self.setData({
					lang_info: data.cutprice_lang_info,
					currency_symbol: app.globalData.get_config('currency_symbol'),
				});
				uni.setNavigationBarTitle({
				    title: self.$t('fengcheche.cutprice.cart_detail_page_title')
				});
				resolve();
			});
		},
        init_real() {
            var self = this;
            uni.showLoading({
                title: this.$t('fengcheche.cutprice.loading_tips')
            });
            this.setData({
                data_list_loding_status: 1
            });
            uni.request({
                url: app.globalData.get_request_url('detail', 'cart', 'cutprice'),
                method: 'POST',
                data: {
                    id: this.params.id
                },
                dataType: 'json',
                success: (res) => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    //console.log(res);

                    if (res.data.code == 0) {
                        var data = res.data.data;
						
						var str = app.globalData.get_page_url();
						str = str.replace('detail/detail','share/share');
						
						var texx = data.data.cut_info.share_ftitle != '' ? data.data.cut_info.share_ftitle : data.plugins_config.share_ftitle;
						var bang = 'linker-cutprice-help-'+data.data.id;
						var title = (data.data.cut_info.share_title != '') ? data.data.cut_info.share_title : data.plugins_config.share_title;
                        self.setData({
                            detail: data.data || null,
                            plugins_config: data.plugins_config,
                            site_fictitious: data.site_fictitious || null,
                            data_list_loding_status: 3,
                            data_bottom_line_status: true,
                            data_list_loding_msg: '',
							// 基础自定义分享
							
							share_info: {
							    title: title,
							    kds: data.data.title,
							    desc: data.data.title,
							    path: '/pages/fcc/cutprice/cart/share/share',
							    query: 'id=' + data.data.id,
							    img: data.data.images,
							    video: data.data.video,
								ext: title+texx+bang+'linker'
							}
                        });
                    } else {
                        self.setData({
                            data_list_loding_status: 2,
                            data_bottom_line_status: false,
                            data_list_loding_msg: res.data.msg
                        });

                        if (app.globalData.is_login_check(res.data, self, 'init')) {
                            app.globalData.showToast(res.data.msg);
                        }
                    }
					
					// 分享菜单处理
					app.globalData.page_share_handle(this.share_info);
                },
                fail: () => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    self.setData({
                        data_list_loding_status: 2,
                        data_bottom_line_status: false,
                        data_list_loding_msg: this.$t('fengcheche.cutprice.request_data_error_tips')
                    });
                    app.globalData.showToast(this.$t('fengcheche.cutprice.request_data_error_tips'));
                }
            });
        },
		
		// h5分享
		share_h5_event() {
			var str = app.globalData.get_page_url();
			str = str.replace('detail/detail','share/share');
			
			var texx = this.detail.cut_info.share_ftitle != '' ? this.detail.cut_info.share_ftitle : this.plugins_config.share_ftitle;
			var bang = 'linker-cutprice-help-'+this.detail.id;
			
		    app.globalData.text_copy_event(texx+bang+'linker'+str);
		},
		
		// 基础分享事件
		share_base_event() {
			var self = this;
		    uni.pageScrollTo({
		        scrollTop: 0,
		        duration: 300,
		        complete: res => {
		            setTimeout(function() {
		                uni.showShareMenu({
							path:'/pages/fcc/cutprice/cart/share/share?id='+self.detail.id
						});
		            }, 500);
		        }
		    });
		},
		// 分享开启弹层
		popup_share_event(e) {
		    if ((this.$refs.share || null) != null) {
		        this.$refs.share.init({
		            status: true,
		            share_info: this.share_info,
					is_special: 1,
		        });
		    }
		},

        // 结算
        buy_submit_event(e) {
			if(!app.globalData.is_single_page_check()) {
			    return false;
			}
			// 在H5或者APP下，如果有跳转购买的链接，则跳转
			// #ifdef APP-PLUS || H5
			var url = this.detail.cut_info.buy_url || null;
			if((url || null) != null)
			{
				url = encodeURIComponent(url);
				app.globalData.open_web_view(url);
				return false;
			}
			// #endif
			
            var ids = e.currentTarget.dataset.id || 0; // 进入订单确认页面
            var data = {
                "buy_type": "cart",
                "ids": ids
            };
			app.globalData.url_open('/pages/fcc/cutprice/buy/buy?data=' + encodeURIComponent(JSON.stringify(data)));
        },

        // 导航事件
        navigation_event(e) {
			// 回到首页，则给自己的帮砍信息清理，不然待会自己也要再次回来这个页面
			// 清理剪贴板
			uni.setClipboardData({
				data: '',
				showToast: false,//不让弹窗提示
				success: function () {}
			});
			
			// 清除对应缓存
			uni.removeStorageSync('linker_cut_data_cache_key');
			
            app.globalData.operation_event(e);
        }
    }
};
</script>
<style>
@import './detail.css';
</style>
